var app = getApp();
Page({

  data: {
    info:[],
    userInfo: null,
    title:'',
    zhicheng:'',
    lid:'9999999',
    
  },

  onLoad: function (options) {
   

    var n=options.scene.split("_");
    //第一个参数
    console.log(n[0])
        //第二个参数
    console.log(n[1])
    this.setData({ lid: options.scene });
    this.getlist(options.scene)


  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{

      this.setData({ userInfo:wx.getStorageSync('userInfo')});

      this.setData({ title:wx.getStorageSync('title')});

      if(wx.getStorageSync('gerenziliao')){

        this.setData({ userinfo:wx.getStorageSync('gerenziliao')});
      }
    

  
    }
  },

  formSubmit: function (event) {
    var that = this;
    if (event.detail.value.phone == '') {
      app.error('手机号不能为空');
      return;
    }
    wx.request({
      url: app.apiUrl + '/api/index/zupdate',
      data: {
        phone: event.detail.value.phone,
        openid:wx.getStorageSync('openid'),
        nickname:wx.getStorageSync('userInfo').nickName
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
    
       if(res.data.msg == 2){
        wx.showModal({
          title: '温馨提示',
          content: '查无此号码,请联系管理员',
          showCancel: false
        });
        return false
       }


       if(res.data.msg == 3){
        wx.showModal({
          title: '温馨提示',
          content: '绑定失败,请联系管理员',
          showCancel: false
        });
        return false
       }

       if(res.data.msg == 4){
        wx.showModal({
          title: '温馨提示',
          content: '该手机号已经绑定过账号,请联系管理员',
          showCancel: false
        });
        return false
       }
       
       if(res.data.msg == 5){
        wx.showModal({
          title: '温馨提示',
          content: '您已绑定过账号,若要绑定新账号请联系管理员先解绑',
          showCancel: false
        });
        return false
       }

       if(res.data.msg == 1){
          console.log(res.data.data)
          wx.showModal({
            title: '温馨提示',
            content: '绑定成功,资料已更新',
            showCancel: true
          });


          wx.setStorageSync('gerenziliao',res.data.data)

          that.setData({ userinfo:res.data.data });
       }
     
      }
    })
 
  },

  getlist:function (lid){
    // var lid = 1

    var that = this
    wx.request({
      url: app.apiUrl + '/api/index/getgongxu',
      data: {
        openid:wx.getStorageSync('openid'),
        id:lid,
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
        if(res.data.code == 1){
          that.setData({ info: res.data.data});
              
        }else{
          wx.showModal({
            title: '温馨提示',

               content: '获取信息失败',
                 showCancel: false
            });
        }
     
      }
    });
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})